package com.sdy.usercenter.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.usercenter.biz.service.SysApplicationService;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.usercenter.biz.model.SysApplication;
import com.sdy.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdy.mvc.controller.BaseController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzq
 * @since 2019-12-12
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/sysApplication")
public class SysApplicationController extends BaseController {
    @Autowired
    private SysApplicationService sysApplicationService;

    /**
     * 查询单条数据
     * @param id 主键
     * @return
     * @throws Exception
     */
    @GetMapping("/get")
    public Response get(Integer id) {
        return Response.success(sysApplicationService.getById(id));
    }

    /**
     * 应用列表
     */
    @GetMapping("/appList")
    public Response appList() {
        List<SysApplication> appList = sysApplicationService.lambdaQuery().orderByAsc(SysApplication::getId).list();
        return Response.success(appList);
    }
    
    /**
     * 分页数据
     * @param request
     * @param page [current, size]
     * @return
     */
    @GetMapping("/pageData")
    public Response pageData(HttpServletRequest request, Page<SysApplication> page, String name, String code) {
        LambdaQueryWrapper<SysApplication> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtil.isNotBlank(name), SysApplication::getName, name);
        wrapper.eq(StringUtil.isNotBlank(code), SysApplication::getCode, code);
        wrapper.orderByDesc(SysApplication::getId);

        IPage<SysApplication> result = sysApplicationService.page(page, wrapper);
        return Response.success(result);
    }

    /**
     * 保存数据
     * @param
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody SysApplication sysApplication) throws BizException {
        sysApplication.setCreateTime(new Date()).setUpdateTime(null);
        sysApplicationService.saveApplication(sysApplication);
        return Response.success();
    }

    /**
     * 更新数据
     * @param
     * @return
     */
    @PostMapping("/update")
    public Response update(@RequestBody SysApplication sysApplication) throws BizException {
        sysApplication.setCreateTime(null).setUpdateTime(null);
        sysApplicationService.updateApplication(sysApplication);
        return Response.success();
    }

    /**
     * 删除单条数据
     * @param id 主键
     * @return
     */
    @GetMapping("/delete")
    public Response delete(Integer id) {
        sysApplicationService.deleteApplication(id);
        return Response.success();
    }
}
